﻿

CREATE proc [Job].[GetJobs]
@ProjectID		int = null,
@CashSaleID		int = null,
@UserID			int = null,
@JobID			uniqueidentifier = null,
@Status			smallint = null
as
begin
	if @ProjectID is not null
	begin
		select	j.JobID,
				j.JobType,
				j.ProjectID,
				j.CashSaleID,
				j.Datum,
				j.Einheit,
				j.EndTime,
				j.Fahrzeug,
				j.Leistung,
				j.LeistungProzent,
				j.LeistungZeit,
				j.ProjektName,
				j.StartTime,
				j.[Status],
				j.Title,
				j.UserID,
				j.Area, 
				j.SubArea, 
				j.Sb, 
				j.Ss, 
				j.Datei, 
				j.Fa, 
				j.Vr
		from	Job.datJob j
		where	j.ProjectID = @ProjectID
		order by j.Datum

		return	0
	end

	if @CashSaleID is not null
	begin
		select	j.JobID,
				j.JobType,
				j.ProjectID,
				j.CashSaleID,
				j.Datum,
				j.Einheit,
				j.EndTime,
				j.Fahrzeug,
				j.Leistung,
				j.LeistungProzent,
				j.LeistungZeit,
				j.ProjektName,
				j.StartTime,
				j.[Status],
				j.Title,
				j.UserID,
				j.Area, 
				j.SubArea, 
				j.Sb, 
				j.Ss, 
				j.Datei, 
				j.Fa, 
				j.Vr
		from	Job.datJob j
		where	j.CashSaleID = @CashSaleID
		order by j.Datum

		return	0
	end

	if @UserID is not null
	begin
		select	j.JobID,
				j.JobType,
				j.ProjectID,
				j.CashSaleID,
				j.Datum,
				j.Einheit,
				j.EndTime,
				j.Fahrzeug,
				j.Leistung,
				j.LeistungProzent,
				j.LeistungZeit,
				j.ProjektName,
				j.StartTime,
				j.[Status],
				j.Title,
				j.UserID,
				j.Area, 
				j.SubArea, 
				j.Sb, 
				j.Ss, 
				j.Datei, 
				j.Fa, 
				j.Vr
		from	Job.datJob j
		where	j.UserID = @UserID
		order by j.Datum

		return	0
	end

	if @JobID is not null
	begin
		select	j.JobID,
				j.JobType,
				j.ProjectID,
				j.CashSaleID,
				j.Datum,
				j.Einheit,
				j.EndTime,
				j.Fahrzeug,
				j.Leistung,
				j.LeistungProzent,
				j.LeistungZeit,
				j.ProjektName,
				j.StartTime,
				j.[Status],
				j.Title,
				j.UserID,
				j.Area, 
				j.SubArea, 
				j.Sb, 
				j.Ss, 
				j.Datei, 
				j.Fa, 
				j.Vr
		from	Job.datJob j
		where	j.JobID = @JobID
		order by j.Datum

		return	0
	end

	if @Status is not null
	begin
		select	j.JobID,
				j.JobType,
				j.ProjectID,
				j.CashSaleID,
				j.Datum,
				j.Einheit,
				j.EndTime,
				j.Fahrzeug,
				j.Leistung,
				j.LeistungProzent,
				j.LeistungZeit,
				j.ProjektName,
				j.StartTime,
				j.[Status],
				j.Title,
				j.UserID,
				j.Area, 
				j.SubArea, 
				j.Sb, 
				j.Ss, 
				j.Datei, 
				j.Fa, 
				j.Vr
		from	Job.datJob j
		where	j.[Status] = @Status
		order by j.UserID, j.Datum

		return	0
	end

	return	-1
end
GO


